
//
//  GetFeedByUserIdTask.m
//  ZooNgonDoiOS
//
//  Created by Anh Jun Linh Tinh on 6/27/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "GetFeedByUserIdTask.h"
#import "RestKit/JSONKit.h"
#import "ZooLikeBean.h"
#import "ZooSpotBean.h"
#import "ZooCheckInBean.h"
#import "ZooFoodBean.h"

const static int ACTION_TYPE_CHECKIN = 1;
const static int ACTION_TYPE_LIKE = 2;

@implementation GetFeedByUserIdTask
-(id)initWithParams:(NSString *)userId limit:(int)limit offset:(int)offset delegate:(id<RKObjectLoaderDelegate>)delegate{
    self =  [super init];
    if(self){
        _userId = userId;
        _limit = &limit;
        _offset = &offset;
        _delegate = delegate;
    }
    return self;
}

-(void)doExecute{
    NSMutableArray *arrKeys = [[NSMutableArray alloc] init];
    NSMutableArray *arrValues = [[NSMutableArray alloc] init];
    if([_userId length]> 0){
        [arrKeys addObject:@"user_id"];
        [arrValues addObject:_userId];
    }
    
    if (_limit > 0) {
        [arrKeys addObject:@"limit"];
        [arrKeys addObject:@"offset"];
        NSString* strLimit = [NSString stringWithFormat:@"%i", _limit];
        NSString* strOffset = [NSString stringWithFormat:@"%i", _offset];
        [arrValues addObject:strLimit];
        [arrValues addObject:strOffset];
    }
    
    NSDictionary *params = [NSDictionary dictionaryWithObjects:arrValues forKeys:arrKeys];
    [[RKObjectManager sharedManager].client get:@"/user/feed" queryParameters:params delegate:self];
}

-(void)request:(RKRequest *)request didReceiveResponse:(RKResponse *)response{
    NSLog(@"%@",[response bodyAsString]);
    NSString *jsonString = [response bodyAsString];
    NSDictionary *json = [jsonString objectFromJSONString];
    NSNumber *status = [json objectForKey:@"status"];
    
    if(status == FALSE){
        return;
    }
    NSArray *feeds = [json objectForKey:@"feeds"];
    int length = [feeds count];
    if(length == 0){
        return;
    }
    for(int i=0;i<length;i++){
        NSDictionary *dict = [feeds objectAtIndex:i];
        int type = [[dict objectForKey:@"type"] intValue];
        
        if(type == ACTION_TYPE_LIKE){
            ZooLikeBean *zooLikeBean = [[ZooLikeBean alloc] init];
            zooLikeBean.time = [[dict objectForKey:@"timestamp"] longValue];
            NSDictionary *spotDic = [dict objectForKey:@"spot"];
            NSString *address = [spotDic objectForKey:@"address"];
            if(address== nil){
                address = @"";
            }
            ZooSpotBean *spotItem = [[ZooSpotBean alloc] init];
            spotItem.address = address;
            spotItem.id_spot = [spotDic objectForKey:@"id"];
            spotItem.name = [spotDic objectForKey:@"name"];
            zooLikeBean.spotBean = spotItem;
            //Ban listener ve..
        }
        else if(type == ACTION_TYPE_CHECKIN){
            ZooCheckInBean *checkIn = [[ZooCheckInBean alloc] init];
            checkIn.time = [[dict objectForKey:@"timestamp"] longValue];
            NSDictionary *dictSpot = [dict objectForKey:@"spot"];
            if(dictSpot != nil){
                NSString *address = [dictSpot objectForKey:@"address"];
                if(address == nil) address = @"";
                ZooSpotBean *spotBean = [[ZooSpotBean alloc] init];
                spotBean.id_spot = [dictSpot objectForKey:@"id"];
                spotBean.name = [dictSpot objectForKey:@"name"];
                spotBean.address = address;
                checkIn.spotBean = spotBean;
            }
            
            NSArray *arrDishes = [dict objectForKey:@"dishes"];
            if(arrDishes== nil){
                for(int i=0,n= [arrDishes count];i<n;i++){
                    ZooFoodBean * foodBean = [[ZooFoodBean alloc] init];
                    NSDictionary *dicFoodBean = [arrDishes objectAtIndex:i];
                    foodBean.name = [dicFoodBean objectForKey:@"name"];
                    foodBean.idItem = [dicFoodBean objectForKey:@"id"];
                    [checkIn addDish:foodBean];
                }
            }
           //Ban listener ve..
        }
    }
    
}

-(void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error{
    NSLog(@"%@",[request.response bodyAsString]);
}

@end
